﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sofire.DataComm.Remoting.Server;
using Sofire.DataComm;

namespace DataCommTest.RemotingTest
{
    public interface IRemoteServer
    {
        double Add(double x, double y);
    }

    public class RemoteServer : IRemoteServer
    {
        #region IRemoteServer 成员

        public double Add(double x, double y)
        {
            return x + y;
        }

        #endregion
    }

    public class RemotingServerTestDemo : ITest
    {
        private void RemotingApplication_ClientFailed(object sender, ClientFailedEventArgs e)
        {
            var cert = RemotingContext.Current.Certificate;
            Console.WriteLine("<<Failed>>Address:{0}\tContractName:{1}\tUsername:{2}\tFailed:{3}"
                , cert.Address
                , cert.ContractName
                , cert.Username
                , e.Exception);
        }

        private void RemotingApplication_ClientInvoked(object sender, ClientInvokedEventArgs e)
        {
            //var cert = RemotingContext.Current.Certificate;
            //Console.WriteLine("<<Invoked>>Address:{0}\tContractName:{1}\tUsername:{2}\tMethodName:{3}"
            //    , cert.Address
            //    , cert.ContractName
            //    , cert.Username
            //    , RemotingContext.Current.Invoking.Method.Name);
            lock(RemotingContext.Current.SessionSyncRoot)
            {
                Console.WriteLine("已经调用 {0} 次", RemotingContext.Current["Index"]);
            }
        }

        private void RemotingApplication_ClientInvoking(object sender, EventArgs e)
        {
            //var cert = RemotingContext.Current.Certificate;
            //Console.WriteLine("<<Invoking>>Address:{0}\tContractName:{1}\tUsername:{2}\tMethodName:{3}"
            //    , cert.Address
            //    , cert.ContractName
            //    , cert.Username
            //    , RemotingContext.Current.Invoking.Method.Name);
            lock(RemotingContext.Current.SessionSyncRoot)
            {
                var index = RemotingContext.Current["Index"];
                if(index == null) RemotingContext.Current["Index"] = 1;
                else RemotingContext.Current["Index"] = (int)index + 1;
            }
        }

        private void RemotingApplication_ClientQuitted(object sender, RemotingContextEventArgs e)
        {
            //if(RemotingContext.Current != null)
            //{
            //    var cert = RemotingContext.Current.Certificate;
            //    Console.WriteLine("<<Quitted>>Address:{0}\tContractName:{1}\tUsername:{2}\tPassword:{3}"
            //        , cert.Address
            //        , cert.ContractName
            //        , cert.Username
            //        , cert.Password);
            //}
        }

        private void RemotingApplication_ClientJoined(object sender, RemotingContextEventArgs e)
        {
            //var cert = RemotingContext.Current.Certificate;
            //Console.WriteLine("<<Joined>>Address:{0}\tContractName:{1}\tUsername:{2}\tPassword:{3}"
            //    , cert.Address
            //    , cert.ContractName
            //    , cert.Username
            //    , cert.Password);
        }

        private void RemotingApplication_ServerStateChanged(object sender, CommunicationStateEventArgs e)
        {
            Console.WriteLine("Server State {0}", e.State);
        }

        private void RemotingApplication_ApplicationStateChanged(object sender, CommunicationStateEventArgs e)
        {
            Console.WriteLine("Application State {0}", e.State);
        }

        public void Test()
        {
            RemotingApplication.Initialization("DefaultSetting.xml");
            RemotingApplication.ApplicationStateChanged += new CommunicationStateEventHandler(RemotingApplication_ApplicationStateChanged);
            RemotingApplication.ServerStateChanged += new CommunicationStateEventHandler(RemotingApplication_ServerStateChanged);
            RemotingApplication.ClientJoined += new RemotingContextEventHandler(RemotingApplication_ClientJoined);
            RemotingApplication.ClientQuitted += new RemotingContextEventHandler(RemotingApplication_ClientQuitted);
            RemotingApplication.ClientInvoking += new EventHandler(RemotingApplication_ClientInvoking);
            RemotingApplication.ClientInvoked += new ClientInvokedEventHandler(RemotingApplication_ClientInvoked);
            RemotingApplication.ClientFailed += new ClientFailedEventHandler(RemotingApplication_ClientFailed);
            RemotingApplication.Open();
        }
    }
}
